<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use Laravel\Lumen\Routing\Controller as BaseController;
use Illuminate\Http\Request;

class testController extends BaseController
{
    /**
     * Redis 测试
     * @param Request $request
     * @return $this|\Illuminate\Http\JsonResponse|mixed|string
     * Date : 2017-07-03
     * Time : 10:37
     * User : Yaowenzhu
     */
    public function redis(Request $request)
    {
        // 获取参数
        $phone = $request->input('phone');
        if(empty($phone)){
            return response()->json(['code'=>'6333','msg'=>'参数错误']);
        }
        // 删除key
        //app('redis')->delete($phone);
        $res = app('redis')->get($phone);

        if(empty($res)){
            // 查询数据库
            /*$res = DB::select(
                'select make,model,year,created_at,updated_at,phone from cars
                 where phone = :phone', ['phone' => $phone]
            );*/
            $res = DB::table('cars')->select('make','model','year','created_at','updated_at','phone')
                   ->where('phone',$phone)->first();
            if($res){
                // 存入缓存系统
                $json_res = json_encode($res);
                app('redis')->set($phone,$json_res);
                return response()->json(['code'=>'0000','msg'=>'请求成功','data'=>$res]);
            }else{
                return response()->json(['code'=>'0033','msg'=>'暂无数据']);
            }
        }else{
            $res = json_decode($res,true);
            return response()->json(['code'=>'0000','msg'=>'请求成功','data'=>$res]);
        }
    }

    /**
     * Memcached 测试
     * @param Request $request
     * @return $this|\Illuminate\Http\JsonResponse|mixed|string
     * Date : 2017-07-03
     * Time : 10:38
     * User : Yaowenzhu
     */
    public function memcached(Request $request)
    {
        // 获取参数
        $phone = $request->input('phone');
        if(empty($phone)){
            return response()->json(['code'=>'6333','msg'=>'参数错误']);
        }
        // 删除缓存
        //$res = app('memcache')->delete($phone);
        // 从缓存中获取数据
        $res = app('memcache')->get($phone);

        if(empty($res)){
            // 查询数据库
            /*$res = DB::select(
                'select make,model,year,created_at,updated_at,phone from cars
                 where phone = :phone', ['phone' => $phone]
            );*/
            $res = DB::table('cars')->select('make','model','year','created_at','updated_at','phone')
                   ->where('phone',$phone)->first();

            if(!empty($res)){
                // 存入缓存系统
                $json_res = json_encode($res);
                app('memcache')->set($phone,$json_res);
                return response()->json(['code'=>'0000','msg'=>'成功','data'=>$res]);
            } else {
                return response()->json(['code'=>'0033','msg'=>'暂无数据']);
            }
        }else{
            $res = json_decode($res,true);
            return response()->json(['code'=>'0000','msg'=>'成功','data'=>$res]);
        }
    }
}
